const city_json = require("../../mock/city_json")
import {Address} from './address-model.js';
var app = getApp();
var address = new Address();
Page({
  data: {
    modalOpened: false,
    addrs: "选择地区",
    telOk: false,
  },
  onLoad(options) {
    this.setData({
      aid:options.aid
    }) 
    // this._loadData();
  },
  onShow()
  {
    this._loadData();
  },
  _loadData:function()
  {
    //根据会员ID获取会员的所有的地址
    address.getUserAddress(app.globalData.uid,(data)=>{
      this.setData({
        addressArr:data.data
      })
    })
    
  },
  // 选择默认地址
  defaultAddr(e) {
   var id = e.currentTarget.dataset.id;
   address.setDefaultAddress(id,(data)=>{
     if(data.code==1)
     {
       my.showToast({
          type: "none",
          content: "设置成功"
        })
        this._loadData();
     }
   })
  },
  // 删除地址
  deleteAddr(e) {
    var id = e.currentTarget.dataset.id;
    my.confirm({
      title:"温馨提示",
      content:"你确定要删除吗",
      confirmButtonText: '确认删除',
      cancelButtonText: '暂不删除',
      success: (res) => {
        if(res.confirm)
        {
        address.deleteAddress(id,(data)=>{
      if(data.code==1)
      {
        my.showToast({
          type:'none',
          content:"删除成功"
        });
        this._loadData();
      }
    })
        }
      },
    });
    
  },
  //添加地址
  openModal(e) {
    this.setData({
      modalOpened: true,
      type:e.currentTarget.dataset.type
    });
    if(e.currentTarget.dataset.type==1)
    {
      this.getAddress(e.currentTarget.dataset.id);
      this.setData({
        id:e.currentTarget.dataset.id
    });
    }else{
      this.setData({
        editArr:'',
        addrs:'选择地区'
      })
    }
  },

  //根据地址ID获取地址详情
  getAddress(id)
  {
    var addressArr = this.data.addressArr;
    for(let i=0;i<addressArr.length;i++)
    {
      if(addressArr[i].id ==id)
      {
        this.setData({
          editArr:addressArr[i],
          addrs:addressArr[i].province + " " + addressArr[i].city + " " +  addressArr[i].region,
          name:addressArr[i].name,
          phone:addressArr[i].phone,
          detail:addressArr[i].detail,
          carid:addressArr[i].carid,
          province:addressArr[i].province,
          region:addressArr[i].region,
          city:addressArr[i].city,
          id:addressArr[i].id

        })
      }
    }
  },

  //获取输入的姓名
  username:function(e)
  {
    this.setData({
      name:e.detail.value
    })
  },
  //获取输入的电话号码
  tel:function(e)
  {
    this.setData({
      phone:e.detail.value
    })
  },
  //获取输入的地址详情
  detail:function(e)
  {
    this.setData({
      detail:e.detail.value
    })
  },
  carId:function(e)
  {
    this.setData({
      carid:e.detail.value
    })
  },

  // 点击弹框的确定按钮
  onModalClick() {
    this.setData({
        modalOpened: false,
      });
  },
  submit:function()
  {
    var that = this;
    if(!that.data.name)
    {
      my.showToast({
          type: "none",
          content: "姓名不能为空"
        })
        return ;
    }
    if(!that.data.phone)
    {
       my.showToast({
          type: "none",
          content: "电话不能为空"
        })
        return ;
    }
    if(!that.data.carid)
    {
      my.showToast({
          type: "none",
          content: "身份证号不能为空"
        })
        return ;
    }
    if(that.data.addrs=='选择地区')
    {
       my.showToast({
          type: "none",
          content: "请选择地址"
        })
        return ;
    }
     if(!that.data.detail)
    {
       my.showToast({
          type: "none",
          content: "详细地址必填"
        })
        return;
    }

    if(that.data.type ==0)
    {
      var params = {
      province:that.data.provice,
      city:that.data.city,
      region:this.data.region,
      detail:that.data.detail,
      name:that.data.name,
      phone:that.data.phone,
      uid:app.globalData.uid,
      carid:that.data.carid
    };
      address.addAddress(params,(data)=>{
      if(data.code ==1)
      {
        that._loadData();
        my.showToast({
          type: "none",
          content: "添加成功"
        })
        that.setData({
          modalOpened:false
        })
      }else if(data.code ==0){
        my.showToast({
         type:'none',
         content:data.msg
        });
      }
    })
    }else{
    
      var params = {
      province:that.data.province,
      city:that.data.city,
      region:that.data.region,
      detail:that.data.detail,
      name:that.data.name,
      phone:that.data.phone,
      id:that.data.id,
      carid:that.data.carid
    };
      address.editAddress(params,(data)=>{
        if(data.code ==1)
        {
           my.showToast({
              type: "none",
              content: "修改成功"
        })
        that._loadData();
        that.setData({
          modalOpened:false
        })
        }else if(data.code ==0)
        {
          my.showToast({
              type: "none",
              content: data.msg
        })
        }
      })
    }
  },
  // 关闭弹框
  onModalClose() {
    this.setData({
      modalOpened: false,
    });
  },
  // 选择地区
  selectAddr() {
    var that = this;

        my.multiLevelSelect({
          title: '选择地区',//级联选择标题
          list: city_json.citys,//引入的js
          success: (res) => {
            let addrs = res.result[0].name + " " + res.result[1].name + " " + res.result[2].name;
            that.setData({
              addrs: addrs,
              provice:res.result[0].name,
              city:res.result[1].name,
              region:res.result[2].name

            })
          }
        });
  },
  inputBlur(e) {
    if (name == "tel") {
      if (/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(e.detail.value)) {
        this.setData({
          tel: e.detail.value,
          telOk: true
        })
      } else {
        my.showToast({
          type: "none",
          content: "手机号输入有误"
        })
      }
    }
  },
checkAddress:function(e)
  {
   
      if(this.data.aid==1)
      { 
        let addressArr = this.data.addressArr;
        let id = e.currentTarget.dataset.id;
        addressArr.forEach((item)=>{
          if(id == item.id)
          {
            my.setStorageSync({
              key: 'addressData', // 缓存数据的key
              data: item, // 要缓存的数据
            });
             my.navigateBack({
                
            });
           
          }
        })
      }
  }
});
